package calorie.com.calorie.activity;

import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.TimeUtils;
import com.lxh.baselibray.util.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.BindView;
import calorie.com.calorie.R;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.SportStastisticsContact;
import calorie.com.calorie.response.SportStastisticsResponce;
import calorie.com.calorie.util.LineChartHelper;
import calorie.com.calorie.util.MaxMinUtils;
import retrofit2.Response;

/**
 * 运动统计
 */
public class SportStastistticsActivity extends MvpActivity<SportStastisticsContact.SportStastisticsPresent> implements SportStastisticsContact.ISportStastisticsView {
    @BindView(R.id.rg)
    RadioGroup rg;
    @BindView(R.id.line_chart)
    LineChart lineChart;
    @BindView(R.id.tv_total_step)
    TextView tvTotalStep;
    @BindView(R.id.tv_total_calorie)
    TextView tvTotalCalorie;
    @BindView(R.id.tv_day_average_heart_rate)
    TextView tvDayAverageHeartRate;
    @BindView(R.id.tv_day_step)
    TextView tvDayStep;
    @BindView(R.id.tv_day_calorie)
    TextView tvDayCalorie;
    int size=7;

    LineChartHelper helper;
    @Override
    public void initData(Bundle savedInstanceState) {
        helper = new LineChartHelper(lineChart,context);
        rg.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.rb_week:
                    size=7;
                    mPresent.querySportsStastistics(context,size);
                    break;
                case R.id.rb_month:
                    size=30;
                    mPresent.querySportsStastistics(context,size);
                    break;
            }
        });
        rg.getChildAt(0).performClick();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_sport_stastistics;
    }


    @Override
    public void getSportsData(Response<SportStastisticsResponce> response) {
        if (response.body().getCode()== Constant.SUCCESS_CODE){
            SportStastisticsResponce.DataBean data = response.body().getData();
            if (ObjectUtils.isEmpty(data))return;
            tvDayAverageHeartRate.setText(data.getAvgHeartRate()+"次");
            tvDayStep.setText(data.getAvgStep()+"步");
            tvDayCalorie.setText(data.getAvgCalorie()+"卡");
            tvTotalStep.setText(data.getStepCount()+"步");
            tvTotalCalorie.setText(data.getCalorieCount()+"卡");

            List<SportStastisticsResponce.DataBean.SportDataBean> sportData = data.getSportData();
            if (ObjectUtils.isEmpty(sportData))return;
            String[] times = new String[sportData.size()];
            Float[] prop = new Float[sportData.size()];
            for (int i = 0; i < sportData.size(); i++) {
                times[i] = TimeUtils.millis2String(sportData.get(i).getTime(), new SimpleDateFormat("MM/dd"));
                prop[i] = Float.valueOf(sportData.get(i).getCalorie());
            }
            MaxMinUtils.MaxMin maxMin = MaxMinUtils.FindMaxAndMin(prop, 0, prop.length - 1);
            float max = maxMin.max;
            float min = maxMin.min;
            float per = (max - min) / prop.length;
            //  float per=0.1f;
            helper.initLineChart(times, max, min, per);
            helper.showLineChart(prop);
        }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }

    @Override
    protected SportStastisticsContact.SportStastisticsPresent createPresent() {
        return new SportStastisticsContact.SportStastisticsPresent();
    }
}
